package com.zxp.cloud.goods.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author shollin
 * @desc
 * @date 2021/7/18/018 9:40
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RefreshScope
public class GoodsController {

    @Value("${author:}")
    public String author;

    private final RestTemplate restTemplate;

    private final LoadBalancerClient  loadBalancerClient;

    @GetMapping("/")
    public String index(String name){

        ServiceInstance serviceInstance = loadBalancerClient.choose("cloud-api-ums");
        int port = serviceInstance.getPort();
        log.info("请求ums的端口为："+port);

        String url = "http://" + serviceInstance.getServiceId() + ":" + serviceInstance.getPort() + "/sayHello?name=" + name;
        log.info(url);
        String result = restTemplate.getForObject(url, String.class);


        String forObject = restTemplate.getForObject("http://cloud-api-ums/", String.class);
        return result + "goods hello:"+author;
    }
}
